package ext.acae2.dms.web.input;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import wt.content.ApplicationData;
import wt.content.ContentHelper;
import wt.content.ContentItem;
import wt.content.ContentRoleType;
import wt.doc.WTDocument;
import wt.fc.QueryResult;
import wt.log4j.LogR;
import wt.pom.Transaction;
import wt.util.WTException;
import wt.util.WTProperties;
import ext.acae2.dms.load.LoadXml;
import ext.acae2.dms.parse.ParseXml;
import ext.acae2.util.DownloadUtil;
import ext.acae2.util.FileUtil;
import ext.acae2.utilities.util.CSCUtil;

/**
 * @desription designer import data by complete work task
 * @author cnie
 * @date 2013-3-21 下午1:52:12
 */
public class DataImportByWorkflow {
	
	static String importPath;
	private static final Logger logger = LogR.getLogger(DataImportByWorkflow.class.getName());
	static {
		try {
		    String tempPath = WTProperties.getLocalProperties().getProperty("wt.temp");
			importPath = tempPath+File.separator+"dataImport";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @description data import
	 * @author cnie
	 * @date 2013-3-21 下午2:07:41
	 * @version 1.0
	 * @param pbo
	 * @throws WTException 
	 */
	public static void importData(Object pbo) throws WTException{
		if(pbo instanceof WTDocument){
			Transaction trans = new Transaction();
			WTDocument doc = (WTDocument)pbo;
			String location = doc.getLocation();
			String[] locationArr = location.split("\\/");
			String supplier = locationArr[3].substring(0,locationArr[3].lastIndexOf("-")-1);
			logger.debug("supplier=========================>"+supplier);
			QueryResult primaryQr = ContentHelper.service.getContentsByRole(doc, ContentRoleType.PRIMARY);
			while(primaryQr.hasMoreElements()){
				ContentItem contentitem = (ContentItem) primaryQr.nextElement();
				ApplicationData applicationdata = (ApplicationData) contentitem;
				String appFileName = applicationdata.getFileName();
				File importFile = new File(importPath);
				if(!importFile.exists()){
					importFile.mkdirs();
				}
				try {
					trans.start();
					DownloadUtil.download(applicationdata, importFile.getAbsolutePath()+File.separator+appFileName);
					File serverFile = new File(importFile.getAbsolutePath()+File.separator+appFileName);
					String filePath = serverFile.getAbsolutePath();
					String xmlPath = "";
					if (filePath.toUpperCase().endsWith(".ZIP")) {
						xmlPath = new UnzipProcessor().unzip(serverFile);
					} else if (filePath.toUpperCase().endsWith(".RAR")) {
						xmlPath = new UnrarProcessor().unrar(serverFile);
					}
					logger.debug("xmlPath: " + xmlPath);
					Map<String, List> resultMap = new ParseXml().execute(xmlPath, supplier);
		            LoadXml.run(resultMap);  //Create Object in Windchill.
					trans.commit();
					if(serverFile.exists()){
						serverFile.delete();
					}
					String unFolder = appFileName.substring(0,appFileName.lastIndexOf("."));
					FileUtil.deleteDir(new File(importPath+File.separator+unFolder));
				} catch (Exception e) {
					if(trans!=null){
						trans.rollback();
					}
					throw new WTException("导出出现错误，请与管理员联系");
				}
			}
			
			
		}
	}
	
	public static void main(String[] args) throws Exception {
		WTDocument doc = (WTDocument) CSCUtil.getObjectByOid("VR:wt.doc.WTDocument:226836");
		importData(doc);
	}

}
